package com.codename1.samples;


import com.codename1.components.ToastBar;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.l10n.L10NManager;
import com.codename1.l10n.ParseException;
import com.codename1.l10n.SimpleDateFormat;
import com.codename1.ui.Button;
import com.codename1.ui.Container;
import com.codename1.ui.TextField;
import com.codename1.ui.table.TableLayout;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class SimpleDateFormatTest {

    private Form current;
    private Resources theme;
    

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        final Form hi = new Form("Hi World", BoxLayout.y());
        TextField dateStringIn = new TextField();
        TextField dateFormat = new TextField();
        Label shortMonth = new Label();
        Label longMonth = new Label();
        Label shortDate = new Label();
        Label longDate = new Label();
        Label dateTime = new Label();
        Label formattedString = new Label();
        Container result = new Container(BoxLayout.y());
        Button parse = new Button("Parse Date");
        Container resultWrapper = new Container(BoxLayout.y());
        
        SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a");

        dateFormatter.format(new Date());
        
        parse.addActionListener(evt->{
            try {
                SimpleDateFormat inputFormat = new SimpleDateFormat(dateFormat.getText());
                Date dt = dateStringIn.getText().length() == 0 ? new Date() : inputFormat.parse(dateStringIn.getText());
                
                SimpleDateFormat shortMonthFormat = new SimpleDateFormat("MMM");
                shortMonth.setText(shortMonthFormat.format(dt));
                
                SimpleDateFormat longMonthFormat = new SimpleDateFormat("MMMM");
                longMonth.setText(longMonthFormat.format(dt));
                
                longDate.setText(L10NManager.getInstance().formatDateLongStyle(dt));
                shortDate.setText(L10NManager.getInstance().formatDateShortStyle(dt));
                dateTime.setText(L10NManager.getInstance().formatDateTime(dt));
                formattedString.setText(inputFormat.format(dt));
                resultWrapper.removeAll();
                resultWrapper.add(result);
                hi.revalidateWithAnimationSafety();
            } catch (ParseException ex) {
                Log.e(ex);
                ToastBar.showErrorMessage("Parse failed: "+ex.getMessage());
            }
        });
        
        
        
        
        result.addAll(
                new Label("Short month name:"),
                shortMonth,
                new Label("Long month name:"),
                longMonth,
                new Label("Short Date:"),
                shortDate,
                new Label("Long Date:"),
                longDate,
                new Label("Time: "),
                dateTime,
                new Label("Formatted String:"),
                formattedString
        );

        hi.addAll(
                new Label("Date:"),
                dateStringIn,
                new Label("Date Format:"),
                dateFormat,
                parse,
                resultWrapper
        );
        
        
        
        
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
